<?php

/**
 * 后台 登录模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: login.class.php 1402 2011-03-03 10:15:34Z zhangwentao $
 * http://www.t-mac.org；
 */
class login extends Model
{

    private $users_table;
    private $userstype_table;
    private $userslog_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->connect();
        $this->users_table = '#@__users';
        $this->userstype_table = '#@__users_type';
        $this->userslog_table = '#@__users_log';
    }

    /**
     * 登录验证
     * @param <type> $admin_name
     * @param <type> $admin_psw
     * @param <type> $yzm
     * @param <type> $remember 
     */
    public function check($admin_name, $admin_psw, $yzm, $remember = 0)
    {
        if (trim(md5($yzm)) != trim($_SESSION['valid'])) {
            $this->redirect("效验码输入错误!", "index.php?m=admin/login");
            exit();
        }
        if (trim($admin_name) == "") {
            $this->redirect("请输入用户名!");
            exit();
        }
        if (trim($admin_psw) == "") {
            $this->redirect("请输入密码!");
            exit();
        }
        $sql = "SELECT a.type_purviews, b.* FROM $this->userstype_table a LEFT JOIN $this->users_table b ON a.rank = b.rank WHERE b.username = '{$admin_name}' LIMIT 0, 1";
        $info = $this->db->getRow($sql);
        if ($info) {
            if (md5(md5($admin_psw)) == $info['password']) {
                if (!empty($_SESSION['admin'])) {
                    session_unregister("admin");
                }
                if (!empty($_SESSION['admin_uid'])) {
                    session_unregister('admin_uid');
                }
                if (!empty($_SESSION['admin_purviews'])) {
                    session_unregister('admin_purviews');
                }
                //注册session
                $_SESSION['admin'] = $info['username'];
                $_SESSION['admin_uid'] = $info['uid'];
                $_SESSION['admin_purviews'] = $info['type_purviews'];

                $uid = $info['uid'];
                $time = time();
                $logip = $_SERVER["REMOTE_ADDR"];
                //插adminlog 登录日志
                $log_info = array(
                    'rank' => $info['rank'],
                    'uid' => $uid,
                    'username' => $info['username'],
                    'logip' => $logip,
                    'logtime' => $time
                );
                $rs = $this->db->autoExecute($this->userslog_table, $log_info, 'INSERT');
                //更新用户上次登录时间，IP，登录次数
                $sql = "UPDATE $this->users_table SET login_ip='$logip', login_time='$time', logincount=logincount+1 WHERE uid = $uid";
                $rs = $this->db->execute($sql);

                if ($remember == 1) {//选中了 记住密码
                    HttpResponse::setcookie("cookie_remember", "yes", time() + 3600 * 24 * 30);
                    HttpResponse::setcookie("cookie_username", $admin_name, time() + 3600 * 24 * 30);
                    HttpResponse::setcookie("cookie_password", $admin_psw, time() + 3600 * 24 * 30);
                } else {
                    HttpResponse::setcookie("cookie_remember", '');
                    HttpResponse::setcookie("cookie_username", '');
                    HttpResponse::setcookie("cookie_password", '');
                }
                $this->redirect("成功登录，正在转向管理管理主页！", 'index.php?m=admin');
                session_unregister('valid');
                exit();
            } else {
                $this->redirect("Access Denied! 密码错误", "index.php?m=admin/login");
                exit();
            }
        } else {
            $this->redirect("Access Denied! 没有此用户", "index.php?m=admin/login");
            exit();
        }
    }

    public function out()
    {
        if (phpversion() < '4.3.0') {
            session_unregister('admin');
            session_unregister('admin_uid');
            session_unregister('admin_purviews');
            session_unregister('valid');
        } else {
            $_SESSION["admin"] = "";
            $_SESSION['admin_uid'] = "";
            $_SESSION['admin_purviews'] = "";
            $_SESSION['valid'] = "";
        }
        $this->redirect("退出/注销成功!", "index.php");
    }

}